package com.googlecode.chigs.web;

import org.apache.wicket.Request;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.chigs.domain.UserSettings;
import com.googlecode.chigs.repository.jpa.UserSettingsDao;
import com.googlecode.chigscommons.web.WicketWebSession;

public class ChigsWebSession extends WicketWebSession {

	private UserSettings userSettings;
	
	@SpringBean
	private UserSettingsDao userSettingsDao;
	
	public ChigsWebSession(Request request) {
		super(request);
		InjectorHolder.getInjector().inject(this);
	}

	public UserSettings getUserSettings() {
		if (userSettings == null && getUser() != null) {
			userSettings = userSettingsDao.findByUser(getUser());
			
			if (userSettings == null) {
				userSettings = new UserSettings();
				userSettings.setUser(getUser());
				userSettings = userSettingsDao.save(userSettings);
			}
		}
		return userSettings;
	}

}
